<template>
  <div class="custom_home">
    <el-container v-loading="loading">
      <el-header>
        <div class="header_item logo">
          <router-link :to="{ path: '/' }"
            ><img src="../assets/logo.png"
          /></router-link>
        </div>
        <div class="header_item search">
          <el-input
            placeholder="请输入内容"
            v-model="searchValue"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
        <div class="header_item other">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="el-dropdown-link">
              <span class="iconfont el-icon-user-solid" v-if="!isLogin"></span>
              <div
                class="img_header"
                v-else
                :style="{
                  'background-image': 'url(' + user.headerImage + ')',
                  'background-size': 'contain',
                }"
              ></div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="goToUser" v-if="isLogin"
                >{{ user.nickName }}({{
                  $store.state.rank[user.rank]
                }})</el-dropdown-item
              >

              <el-dropdown-item divided command="login" v-if="!isLogin"
                >登录</el-dropdown-item
              >
              <el-dropdown-item command="reg" v-if="!isLogin"
                >注册</el-dropdown-item
              >
              <el-dropdown-item divided command="editPassword" v-if="isLogin"
                >修改密码</el-dropdown-item
              >
              <el-dropdown-item command="set" v-if="isLogin"
                >设置</el-dropdown-item
              >
              <el-dropdown-item command="quit" v-if="isLogin"
                >退出</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside :style="{ width: isCollect ? 'auto' : '200px' }">
          <div class="custom_home_left_top_collect">
            <span
              class="button"
              :class="!isCollect ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
              @click="collectClickHandle"
            ></span>
          </div>
          <RightMenu
            :collapse="isCollect"
            @addTabHandle="addTabHandle"
            :menuData="menuData"
          ></RightMenu>
        </el-aside>
        <el-main>
          <el-tabs
            :tab-position="'top'"
            :closable="true"
            @tab-remove="tabRemoveHandle"
            type="card"
            v-model="editableTabsValue"
          >
            <el-tab-pane
              :label="tab.title"
              v-for="(tab, index) in tabs"
              :name="'tab_' + index"
              :key="'leftTab_' + index"
            >
              <div class="custom_home_tab_content">
                <component :is="tab.component" :data="tab" />
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-main>
      </el-container>
      <el-footer style="height: 30px">&copy; Copyright. test 2021.</el-footer>
    </el-container>
  </div>
</template>
<script>
import eventHub from "../plugins/eventHub";
export default {
  name: "home",
  components: {
    RightMenu: () =>
      import(
        /* webpackChunkName:'rightMenu' */ "../components/right-menu/right-menu.vue"
      ),
  },
  data() {
    return {
      isCollect: false,
      loading: false,
      menuData: {},
      editableTabsValue: "tab_0",
      searchValue: "",
      isLogin: false,
      user: {},
      tabs: [
        {
          title: "welcome",
          name: "tab_0",
          component: () =>
            import(
              /* webpackChunkName:'welcome' */ "../components/welcome.vue"
            ),
        },
      ],
    };
  },
  created() {
    if (!this.$token.has()) {
      this.$router.push("/login");
      return;
    }
    this.isLogin = true;
    let that = this;
    that.loading = true;
    Promise.all([this.getMenuData(), this.getUserInfo()])
      .then((v) => {
        that.menuData = v[0].data;
        that.user = v[1].data;
      })
      .catch((v) => {
        console.log(v);
      })
      .finally(() => {
        that.timeout = clearTimeout(that.timeout);
        that.timeout = setTimeout(() => (that.loading = false), 500);
      });
  },
  mounted() {
    eventHub.$on("addTabHandle", this.addTabHandle);
    eventHub.$on("closeTabHandle", this.closeTabHandle);
  },
  beforeDestroy() {
    this.timeout && clearTimeout(this.timeout);
  },
  methods: {
    closeTabHandle(v) {
      this.tabRemoveHandle("tab_" + v);
    },
    toLoginClickHandle() {
      this.$router.push("/login");
    },
    handleCommand(v) {
      console.log(v);
      let that = this;
      switch (v) {
        case "goToUser":
          break;
        case "editPassword":
          break;
        case "quit":
          this.$confirm("是否退出登录?", "提示", {
            confirmButtonText: "退出登录",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              that.$token
                .logout({})
                /* this.$store
                .dispatch("quitLogin") */
                .then(() => {
                  that.$router.push("/login");
                })
                .catch((v) => {
                  that.$message("退出登录失败");
                  console.log(v);
                });
            })
            .catch(() => {});
          break;
        case "set":
          break;
        case "login":
          break;
        case "reg":
          break;
      }
    },
    collectClickHandle() {
      this.isCollect = this.isCollect ? false : true;
    },
    getUserInfo() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$user
          .getUserInfo()
          /* that.$store
          .dispatch("getUserInfo") */
          .then((v) => {
            resolve(v);
          })
          .catch((v) => {
            console.log(v);
            that.$message(v.message || "Error");
            reject(v);
          }) /* 
          .finally(() => {
            that.timeout = clearTimeout(that.timeout);
            that.timeout = setTimeout(() => (that.loading = false), 500);
          }) */;
      });
    },
    getMenuData() {
      let that = this;
      //this.loading = true;
      return new Promise((resolve, reject) => {
        that.$store
          .dispatch("getMenuData")
          .then((v) => {
            resolve(v);
          })
          .catch((v) => {
            console.log(v);
            that.$message(v.message || "Error");
            reject(v);
          });
        /* .finally(() => {
            that.timeout = clearTimeout(that.timeout);
            that.timeout = setTimeout(() => (that.loading = false), 500);
          }) */
      });
    },
    tabRemoveHandle(v) {
      v = parseFloat(v.replace("tab_", ""));
      this.tabs.splice(v, 1);
      this.editableTabsValue =
        "tab_" + (this.tabs.length - 1 < 0 ? 0 : this.tabs.length - 1);
    },
    addTabHandle(v) {
      console.log(v);
      let number = this.$Plugins.hasRouter(this, v.router);
      if (number !== false) {
        this.editableTabsValue = "tab_" + number;
      } else {
        let component = this.$Plugins.getRouteComponent(this, v.router);
        if (!component) return;
        this.tabs.push({
          title: v.title,
          component: component.default,
          router: v.router,
          name: "tab_" + this.tabs.length,
          data: v.data || {},
          tabIndex: this.tabs.length - 1,
        });
        this.editableTabsValue = "tab_" + (this.tabs.length - 1);
      }
    },
  },
};
</script>
<style lang="less">
.custom_home {
  width: 100%;
  height: 100%;
  text-align: left;
  .el-container {
    width: 100%;
    height: 100%;
    .el-header {
      height: 30px;
      background-color: #0e53a7;
      color: #fff;
      display: flex;
      flex-direction: row;
      .header_item {
        &.logo {
          flex: 6;
          display: flex;
          align-items: center;
          a {
            background-color: #efefef;
            width: 99px;
            height: 31px;
            overflow: hidden;
            display: block;
            img {
              width: 100%;
              height: 100%;
            }
          }
        }
        &.search {
          flex: 2;
          display: flex;
          align-items: center;
          .el-input__inner {
            height: 30px;
            line-height: 30px;
          }
        }
        &.other {
          width: 60px;
          display: flex;
          justify-content: center;
          align-items: center;
          color: #fff;
          text-align: center;
          .el-dropdown {
            color: #fff;
            cursor: pointer;
            flex: 9;
          }
          .iconfont {
            flex: 1;
            font-size: 22px;
            padding: 5px;
            background-color: #fff;
            color: #999;
            border-radius: 22px;
          }
          .img_header {
            width: 40px;
            height: 40px;
            overflow: hidden;
            border-radius: 100%;
            margin: 0 0 0 10px;
            border: 2px #ffffff solid;
            background-color: #999;
          }
        }
      }
    }
    .el-aside {
      position: relative;
      display: flex;
      flex-direction: column;
      .custom_home_left_top_collect {
        background-color: #0e53a7;
        cursor: pointer;
        font-size: 22px;
        padding: 5px;
        text-align: right;
        .button {
          color: #fff;
          display: inline-block;
        }
      }
      padding: 0 0;
      background-color: #fff;
    }
    .el-main {
      padding: 5px 0;
      .el-tabs--card > .el-tabs__header .el-tabs__nav {
        margin: 0 0 0 20px;
      }
      .custom_home_tab_content {
        padding: 0 20px;
      }
    }
    .el-footer {
      background-color: #efefef;
      line-height: 30px;
      text-align: left;
      color: #999;
    }
  }
}
</style>
